package ui.panel {

	import beans.TowerBean;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import globals.Global;
	
	import ui.component.Grid;
	import ui.component.Page;
	import ui.panel.towers.TowerInfo;
	
	public class TowerPanel extends BasePanel {
	
		private var towerList:Array;
		private var gridList:Array;
		public var buildPoint:Point;
		private var page:Page;

		public function TowerPanel() {
			super();
			pid = PanelType.TOWER_PANEL;
			updateTitle("建设新炮塔");
		}
		
		override protected function initUI():void {
			super.initUI();
//			var bg:Bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("grid_bg", "ui"));
//			bg.x = 200;
//			bg.y = 50;
//			addChild(bg);
			
			towerList = new Array();
			var towerInfo:TowerInfo;
			for (var i:int = 0; i<4; i++) {
				towerInfo = new TowerInfo();
				towerInfo.x = 10 + i * 148;
				towerInfo.y = 50;
				addChild(towerInfo);
				towerList.push(towerInfo);
			}
			
//			var i:int;
//			var grid:Grid;
//			gridList = new Array();
//			var gap:uint = Grid.SIZE + 8;
//			for (i = 0; i<16; i++) {
//				if (i < 2) {
//					grid = new Grid("2001");
//				} else {
//					grid = new Grid();
//				}
//				grid.bag = PanelType.TOWER_PANEL;
//				grid.index = i;
//				grid.handler = gridHandler;
//				grid.x = i % 4 * gap + 256;
//				grid.y = int(i / 4) * gap + 70;
//				addChild(grid);
//				gridList.push(grid);
//			}
//			
//			addEventListener(MouseEvent.CLICK, clickHandler);
			
			page = new Page(pageHandler);
			page.update(4);
			page.x = 300;
			page.y = 396;
			addChild(page);
			
			var bean:TowerBean = new TowerBean();
			bean.image = "tower1b";
			bean.name = "冰冻";
			bean.id = 123;
			bean.desc = "asdmasdjiasod";
			bean.range = 10;
			bean.interval = 10;
			updateTower(bean);
		}
		
		public function updateTower(bean:TowerBean):void {
			for each (var towerInfo:TowerInfo in towerList) {
				towerInfo.update(bean);
			}
		}
		
		private function clickHandler(event:MouseEvent):void {
			if (event.target is Grid) {
				var grid:Grid = event.target as Grid;
				gridHandler(grid.bag, grid.index);
			}
		}
		
		private function gridHandler(bag:uint, index:uint):void {
			Global.uiManager.enterBuildState();
			close();
		}
		
		private function pageHandler(num:uint):void {
			trace(num);
		}
		
		public function clear():void {
			for each (var towerInfo:TowerInfo in towerList) {
				towerInfo.clear();
			}
			var grid:Grid;
			for (var i:int = 0; i<16; i++) {
				grid = gridList[i];
				grid.clear();
			}
		}
	}
}